#ifndef  __ANTX__DEF_H___
#define  __ANTX__DEF_H___ 

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <event.h>
#include <netdb.h>
#include <string.h>
#include <string>

#define MULTI_END_KEY                "BIND-RESULT"
#define PACKAGE                      "nantx"
#define VERSION                      "2009-06-17-01"
#define DATA_BUFFER_SIZE             2048
#define UDP_READ_BUFFER_SIZE         65536
#define UDP_MAX_PAYLOAD_SIZE         1400
#define UDP_HEADER_SIZE              8
#define MAX_SENDBUF_SIZE             (256 * 1024 * 1024)
/* I'm told the max legnth of a 64-bit num converted to string is 20 bytes.
 * Plus a few for spaces, \r\n, \0 */

/** Initial size of the sendmsg() scatter/gather array. */
#define IOV_LIST_INITIAL             400
/** Initial number of sendmsg() argument structures to allocate. */
#define MSG_LIST_INITIAL             10
/** High water marks for buffer shrinking */
#define READ_BUFFER_HIGHWAT          8192
#define IOV_LIST_HIGHWAT             600
#define MSG_LIST_HIGHWAT             100

/* Get a consistent bool type */
# include <stdbool.h>
# include <stdint.h>
# include <unistd.h>

/** Time relative to server start. Smaller than time_t on 64-bit systems. */
//typedef unsigned int rel_time_t;
//#define MAX_VERBOSITY_LEVEL 2

struct settings {
    int maxconns;
    int port;
    int udpport;
    char *inter;
    char *socketpath;   /* path to unix socket if using local socket */
    int access;  /* access mask (a la chmod) for unix domain socket */
    int num_threads;        /* number of libevent threads to run */
    int detail_enabled;     /* nonzero if we're collecting detailed stats */
    int reqs_per_event;     /* Maximum number of io to process on each io-event. */
    int backlog;
    char * authfile;
    char * helper;
    int timer;
};


#define ITEM_LINKED 1
#define ITEM_DELETED 2
/* temp */
#define ITEM_SLABBED 4

enum conn_states {
    conn_listening,  /** the socket which listens for connections */
    conn_read,       /** reading in a command line */
    conn_write,      /** writing out a simple response */
    conn_nread,      /** reading in a fixed number of bytes */
    conn_swallow,    /** swallowing unnecessary bytes w/o storing */
    conn_closing,    /** closing this connection */
    conn_mwrite,     /** writing out many items sequentially */
};

#define NREAD_ADD 1
#define NREAD_SET 2
#define NREAD_REPLACE 3
#define NREAD_APPEND 4
#define NREAD_PREPEND 5
#define NREAD_CAS 6

typedef struct conn conn;
struct conn {
    int    sfd;
    int    state;
    struct event event;
    short  ev_flags;
    short  which;   /** which events were just triggered */

    char   *rbuf;   /** buffer to read commands into */
    char   *rcurr;  /** but if we parsed some already, this is where we stopped */
    int    rsize;   /** total allocated size of rbuf */
    int    rbytes;  /** how much data, starting from rcur, do we have unparsed */

    char   *wbuf;
    char   *wcurr;
    int    wsize;
    int    wbytes;
    int    write_and_go; /** which state to go into after finishing current write */
    void   *write_and_free; /** free this memory after finishing writing */

    char   *ritem;  /** when we read in an item's value, it goes here */
    int    rlbytes;
    int    vlen;
    int    offset;

    /* data for the nread state */

  //    /* for commands set/add/replace  */
    char *key;
    int nkey;
    int flags;
    time_t expire;

    /* data for the swallow state */
    int    sbytes;    /* how many bytes to swallow */

    /* data for the mwrite state */
    struct iovec *iov;
    int    iovsize;   /* number of elements allocated in iov[] */
    int    iovused;   /* number of elements used in iov[] */

    struct msghdr *msglist;
    int    msgsize;   /* number of elements allocated in msglist[] */
    int    msgused;   /* number of elements used in msglist[] */
    int    msgcurr;   /* element in msglist[] being transmitted now */
    int    msgbytes;  /* number of bytes in current msg */
/*
    item   **ilist;  
    int    isize;
    item   **icurr;
    int    ileft;
    char   **suffixlist;
    int    suffixsize;
    char   **suffixcurr;
    int    suffixleft;
*/

    /* data for UDP clients */
    bool   udp;       /* is this is a UDP "connection" */
    int    request_id; /* Incoming UDP request ID, if this is a UDP "connection" */
    struct sockaddr request_addr; /* Who sent the most recent request */
    socklen_t request_addr_size;
    unsigned char *hdrbuf; /* udp packet headers */
    int    hdrsize;   /* number of headers' worth of space is allocated */

    int    binary;    /* are we in binary mode */
    bool   noreply;   /* True if the reply should not be sent. */
    conn   *next;     /* Used for generating a list of conn structures */
};

////////////


typedef struct respHeader_s{
    std::string header;
} respHeader_s;

#endif  //  __ANTX__H___
